<script>

var idDisablePedido = 0;
var lastClick = "";

function showOpt(id)
{
	$(".opt_" + id).slideDown("fast");

	hideOpt(lastClick);
}

function hideOpt(id)
{
	$(".opt_" + id).slideUp("fast");
}

function openPedido(mesa)
{
	$("#general").hide();

	$.post('<?php echo $this->base; ?>/pedido/pedido', {cod_mesa: mesa}, function(req)
	{
		$("#editPedido").html(req).fadeIn("fast");
	});
}

function openHistorico(id)
{
	$("#desac_btn").hide();

	closePedido(id);
}

function disactivePedido()
{
	if(confirm("Deseja fecha o pedido?"))
	{
		$.post('<?php echo $this->base; ?>/pedido/disactive', {cod_pedido: idDisablePedido}, function(req)
		{
			$(".bg_transp").hide();
			$(".resumDiv").fadeOut("fast");
			
			window.open("<?php echo $this->base; ?>/imprimir/imprimir/cod/" + idDisablePedido, "_blank");
			
			$("#menu_cnew").click();
	
			//$("#editPedido").html(req).fadeIn("fast");
		});
	}
}

function closePedido(id)
{
	idDisablePedido = id;
	
	$(".bg_transp").show();

	$.post('<?php echo $this->base; ?>/pedido/list', {cod: id}, function(res)
	{
		$("#resumList").html(res);
		$(".resumDiv").fadeIn("fast");
	});
}

function printReceive()
{
	window.open("<?php echo $this->base; ?>/imprimir/imprimir/cod/" + idDisablePedido, "_blank");
}

</script>

<div id="general" style="width: 1000px; margin: 0 auto; overflow: auto;">
	<div style="margin-bottom: 10px; height: 10px;">
		<div style="float: left;">
			<input type="checkbox" onclick="$('.listmesa').fadeIn('fast');" />
		</div>
		<div style="float: left; width: 300px; margin-top: 3px;">
			Mostrar todas mesas<label id='load_subproduct' style='display: none;'>&nbsp;<img src='$this->base/public/images/ajax-loader.gif' /></label>
		</div>
	</div>
	<hr />
	<?php 
	
	if(!$this->arr_pedido)
	{
		die("<div style='text-align: center; margin-top: 150px;'><i>Nenhuma mesa em uso.</i></div>");
	}
	
	foreach ($this->arr_pedido as $key => $val)
	{
		$arr[] = $val['caf02_mesa'];
		$user[$val['caf02_mesa']] = $val['caf01_user'];
		$user_cod[$val['caf02_mesa']] = $val['caf01_cod'];
		$cod[$val['caf02_mesa']] = $val['caf02_cod'];
	}
	
	for($i = 1; $i < 40; $i++)
	{
		$used = "";
		$bg = "";
		
		if(array_search($i, $arr) === false)
		{
		?>
		<div class="listmesa" style="float: left; display: none;">
			<div onclick="showOpt(<?php echo $i; ?>); lastClick = '<?php echo $i; ?>';" class="button ui-corner-all" type="button" style="font-size: 12px; padding-top: 30px; text-align: center; width: 90px; height: 70px; margin: 5px; border: 1px solid gray;">
				Mesa <?php echo $i; ?>
			</div>
			<div style="opacity: 0.9; position: absolute; z-index: 999; display: none; text-align: left; font-size: 24px; width: 166px; border: 1px solid #B0ADAD; background-color: white; margin-left: 3px;" class="ui-widget ui-corner-all unit opt_<?php echo $i; ?>" >
				<div onclick="openHistorico('<?php echo $i; ?>');" style="border-bottom: 1px solid #B0ADAD; padding: 5px;">
					Historico
				</div>
			</div>
		</div>
		<?
		}
		else 
		{
		?>
		<div class="listmesa" style="float: left; <?php echo $bg; ?>">
			<div onclick="showOpt(<?php echo $i; ?>); lastClick = '<?php echo $i; ?>';" class="button ui-corner-all" type="button" style="font-size: 12px; padding-top: 30px; text-align: center; width: 90px; height: 70px; margin: 5px; border: 1px solid gray;">
				Mesa <?php echo $i; ?>
				<font style="font-weight: bold;">
				&nbsp;
				<?php
				if($this->userId == $user_cod[$i] || $this->gerente)
				{
					echo $user[$i];
				} 
				?>
				</font>
			</div>
			<div style="opacity: 0.9; position: absolute; z-index: 1; display: none; text-align: left; font-size: 24px; width: 166px; border: 1px solid #B0ADAD; background-color: white; margin-left: 4px; margin-top: -4px;" class="ui-widget ui-corner-all unit opt_<?php echo $i; ?>" >
				<div onclick="edpedido = 1; openPedido('<?php echo $cod[$i]; ?>');" style="border-bottom: 1px solid #B0ADAD; padding: 5px;">
					Itens
				</div>
				<div onclick="edpedido = 1; openPedido('<?php echo $cod[$i]; ?>');" style="border-bottom: 1px solid #B0ADAD; padding: 5px;">
					Historico
				</div>
				<div onclick="edpedido = 1; openPedido('<?php echo $cod[$i]; ?>');" style="border-bottom: 1px solid #B0ADAD; padding: 5px;">
					Imprimir
				</div>
				<div onclick="closePedido('<?php echo $cod[$i]; ?>');" style="border-bottom: 1px solid #B0ADAD; padding: 5px;">
					Fechar Mesa
				</div>
			</div>
		</div>
	<?
		}
	}
	?>
	
	<div style="margin-top: 400px;">
	
	</div>
	<!-- 
	<a href="#" onclick="$('#allpedidos').fadeIn('fast');" style="color: blue;">[+]Ver todos pedidos</a>
	<hr />
	<div id="allpedidos" style="display: none;">
		<table id="tbl_list" width="100%" style="font-size: 12px;">
			<?php 
			
			$total_geral = 0;
			$mesa        = 0;
			
			foreach ($this->arr_pedido as $key => $val)
			{
				$total_prod  = $val["caf05_valor"] * $val["caf04_qtd"];
				$total_geral += $total_prod;
				
				if($mesa != $val['caf02_mesa'] && $mesa)
				{
					echo "<tr><td colspan='5'>Mesa: {$val['caf02_mesa']}</td></tr>";
				}
				
				if($mesa == 0)
				{
					echo "<tr><td colspan='5'>Mesa: {$val['caf02_mesa']}</td></tr>";
				}
			?>
				<tr>
					<td width="1%">
						<?php echo $val["caf04_qtd"]; ?>
					</td>
					<td>
						<?php echo "{$val["caf05_prod"]}-{$val["caf05_car"]}"; ?>				
					</td>
					<td width="1%">
						$<?php echo $val["caf05_valor"]; ?>/un.&nbsp;&nbsp;
					</td>
					<td width="1%">
						$<?php echo $total_prod; ?>	
					</td>
					<td width="1%">
						<input type="button" value="Excluir" />
					</td>
				</tr>
			<?
				$mesa = $val['caf02_mesa'];
			}
			
			?>
		</table>
	</div>
	-->
</div>
<div style="display: none;" id="editPedido"></div>
<div class="bg_transp" style="display: none; z-index: 2; position: fixed; width: 100%; height: 100%; opacity: 0.8; background-color: white;  top: 0; left: 0;"></div>

<div class="resumDiv ui-corner-all" style="border: 1px solid gray; z-index: 3; background-repeat: repeat-x; padding: 10px; display: none; margin-left: -150px; left: 50%; position: fixed; top: 50px; width: 300px; background-color: white;">
	<input id="desac_btn" type="button" onclick="disactivePedido();" value="Fechar Mesa" style="padding: 10px; width: 300px;" />
	<input type="button" onclick="printReceive();" value="Imprimir" style="padding: 10px; width: 150px;" /><input type="button" onclick="$('.bg_transp').hide(); $('.resumDiv').fadeOut('fast');" value="Cancelar" style="padding: 10px; width: 150px;" />
	<hr />
	<div id="resumList">
	</div>
</div>